 /**
 * Description: 盘点计划
 * @author:hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   hunter       1.0.0     Initial
 */
package com.metro.paperless.app.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.app.beans.vo.AppCheckDetail;
import com.metro.paperless.app.beans.vo.AppCheckDetailForm;
import com.metro.paperless.app.beans.vo.AppOutDetail;
import com.metro.paperless.app.mapper.IAppCheckDetailMapper;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.mapper.IBizMaterialsManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.app.mapper.IAppCheckPlanMapper;
import com.metro.paperless.app.service.inf.IAppCheckPlanService;
import com.metro.paperless.app.beans.vo.AppCheckPlan;

import java.util.Date;
import java.util.List;

 /**
 * 盘点计划<br>
 *
 * @author:hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("appcheckplanService")
public class AppCheckPlanService extends BaseServiceSupport<AppCheckPlan> implements IAppCheckPlanService{
    //盘点计划Mapper
    @Autowired
    private IAppCheckPlanMapper appcheckplanMapper;
    //盘点明细
    @Autowired
    private IAppCheckDetailMapper appCheckDetailMapper;
     @Autowired
     private IBizMaterialsManageMapper materialsManageMapper;

    @Override
    public IMapper getMapper() {
        return appcheckplanMapper;
    }

    @Override
    public IResult save(AppCheckPlan entity) {
        try {
            appcheckplanMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增盘点计划失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
     public IResult update(AppCheckPlan appCheckPlan) {
         try {
             appCheckPlan.setCheckStatus("3");//已经完成
             appcheckplanMapper.update(appCheckPlan);
             List<AppCheckDetail> details = appCheckDetailMapper.queryListByCheckId(appCheckPlan.getId());

             for (AppCheckDetail detailEntity : details) {

                 Long matId = detailEntity.getMaterialsId();
                 BizMaterialsManage materialsManage = materialsManageMapper.getById(matId);
                 int checkNum = detailEntity.getCheckStock(); //盘点库存
                 int enableNum = materialsManage.getEnableStockNum() - (materialsManage.getStockNum() - checkNum); //计算后的可用库存
                 materialsManage.setStockNum(checkNum);
                 materialsManage.setEnableStockNum(enableNum);
                 materialsManageMapper.updateNotEmpty(materialsManage);

             }
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("修改盘点明细失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
    public IResult update(AppCheckDetailForm itemEntity,AppCheckPlan entity) {
        try {
            if(entity.getCheckStatus().equals("1") ){
                Long palnId = entity.getId();
                appCheckDetailMapper.deleteByPlanIds(palnId);
                for (AppCheckDetail detailEntity : itemEntity.getItemEntity()) {
                    detailEntity.setCheckPlanId(palnId);
                    appCheckDetailMapper.save(detailEntity);
                }
                appcheckplanMapper.updateNotEmpty(entity);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改盘点计划失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
    public IResult updateCheck (AppCheckDetailForm itemEntity,AppCheckPlan entity) {
        try {
            if(entity.getCheckStatus().equals("1") ){
                for (AppCheckDetail detailEntity : itemEntity.getItemEntity()) {
                    Long id = detailEntity.getId();
                    AppCheckDetail checkDetail = appCheckDetailMapper.getById(id);
                    checkDetail.setCheckStock(detailEntity.getCheckStock());
                    checkDetail.setRemark(detailEntity.getRemark());
                    BizMaterialsManage materialsManage = materialsManageMapper.getById(detailEntity.getMaterialsId());
                    int stockNum = materialsManage.getStockNum();
                    //保存库存差异
                    if(stockNum == detailEntity.getCheckStock()){
                        checkDetail.setDiffStock("1");
                    }else if(stockNum > detailEntity.getCheckStock() ){
                        checkDetail.setDiffStock("2");
                    }else{
                        checkDetail.setDiffStock("3");
                    }
                    appCheckDetailMapper.updateNotEmpty(checkDetail);
                }
                entity.setCheckStatus("2");
                entity.setCheckTime(new Date());
                appcheckplanMapper.updateNotEmpty(entity);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改盘点计划失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }





    @Override
    public IResult delete(Long id) {
        try {
            appcheckplanMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            appcheckplanMapper.deleteByIds(ids);
            appCheckDetailMapper.deleteByPlanIds(Long.valueOf(ids));
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
     public JsonResult save(AppCheckDetailForm itemEntity, AppCheckPlan entity) {
         try {
             JsonResult jr = new JsonResult();
             entity.setInitiatorTime(new Date());
             entity.setCheckStatus("1");
             appcheckplanMapper.save(entity);
             Long checkId = entity.getId();
             for (AppCheckDetail itementity : itemEntity.getItemEntity()) {
                 itementity.setCheckPlanId(checkId);
                 itementity.setPlanCode(entity.getCheckCode());
                 appCheckDetailMapper.save(itementity);
             }
             jr.setData(checkId);
             jr.setResult(ResultCode.SUCCESS);
             return jr;
         } catch (Exception e) {
             log.error("新增物资出库记录失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }
 }